package com.oig.sys.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.oig.common.constants.BasicConstants;
import com.oig.common.constants.CommonEnum;
import com.oig.sys.base.dao.ITbCurrRateHisDao;
import com.oig.sys.base.entity.TbCurrRate;
import com.oig.sys.base.service.ITbCurrRateHisService;
import com.oig.sys.base.entity.TbCurrRateHis;

import com.oig.sys.base.vo.CurrRateHisVo;
import com.oig.sys.base.vo.CurrRateVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.YearMonth;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author wuxl
 */
@Service
public class TbCurrRateHisServiceImpl implements ITbCurrRateHisService {

	private final ITbCurrRateHisDao tbCurrRateHisDao;

	@Autowired
	public TbCurrRateHisServiceImpl( ITbCurrRateHisDao tbCurrRateHisDao){
		 this.tbCurrRateHisDao = tbCurrRateHisDao;
	}

	@Transactional
	@Override
	public void addTbCurrRateHis(TbCurrRate currRate) {
		TbCurrRateHis his = new TbCurrRateHis();
		BeanUtils.copyProperties(currRate, his,"id");
		his.setHisTime(new Date());
		this.tbCurrRateHisDao.save(his);
    }

	@Transactional
	@Override
	public void addTbCurrRateHis(List<CurrRateVo> currRateVos) {
		for (CurrRateVo currRateVo : currRateVos) {
			TbCurrRateHis his = new TbCurrRateHis();
			BeanUtils.copyProperties(currRateVo, his, "id");
			his.setHisTime(new Date());
			this.tbCurrRateHisDao.save(his);
		}
	}

	@Override
	public List<CurrRateHisVo> queryCurrRateByCodeAndDate(List<String> currencyList, String date) {
		List<TbCurrRateHis> currRateHisList = tbCurrRateHisDao.findAllByCurrAndDate(currencyList, date);
		List<CurrRateHisVo> voList = new ArrayList<>();
		if (!CollectionUtils.isEmpty(currRateHisList)) {
			currRateHisList.forEach((currRateHis) -> {
				CurrRateHisVo currRateVo = new CurrRateHisVo();
				BeanUtils.copyProperties(currRateHis, currRateVo);
				voList.add(currRateVo);
			});
		}
		return voList;
	}


	@Override
	public List<CurrRateVo> queryCurrRateByCodeAndYearMonth(List<String> currencyList, YearMonth yearMonth) {
		List<TbCurrRateHis> customsRateList = new ArrayList<>();
		if (currencyList.contains(BasicConstants.DEFAULT_CURR)) {
			//人民币没有yearMonth
			List<TbCurrRateHis> rmbRate = tbCurrRateHisDao.findAllByCurrency(BasicConstants.DEFAULT_CURR);
			customsRateList.addAll(rmbRate);
		}

		customsRateList.addAll(tbCurrRateHisDao.findAllByCurrAndYearMonth(currencyList, yearMonth.toString(),
				CommonEnum.FlagEnum.TRUE_FLAG.getCode()));
		Set<String> currSet = new HashSet<>();
		List<TbCurrRateHis> result = new ArrayList<>();
		for (TbCurrRateHis tbCurrRateHis : customsRateList) {
			boolean add = currSet.add(tbCurrRateHis.getCurrency());
			if (add) {
				result.add(tbCurrRateHis);
			}
		}

		return BeanUtil.copyToList(result, CurrRateVo.class);
	}
}
